package com.ty.service.impl;

import com.ty.bean.ProductCategory;
import com.ty.common.crud.BaseServiceImpl;
import com.ty.mapper.ProductCategoryMapper;
import com.ty.service.ProductCategoryService;
import org.springframework.stereotype.Service;

@Service
public class ProductCategoryServiceImpl extends BaseServiceImpl<ProductCategory, ProductCategoryMapper> implements ProductCategoryService {

    @Override
    public int save(ProductCategory entity) {
        entity.setCode(getMaxCode(entity.getPid()));
        return super.save(entity);
    }

    /**
     * 获取新的编码
     *
     * @param pid
     * @return
     */
    String getMaxCode (int pid){
        String maxCode = null;
        maxCode = baseMapper.queryMaxCode(pid);
        if (null == maxCode){
            if (pid==0) {
                //添加一级编码
                return "10";
            }else {
                //不存在 查询id=entity.getPid()的编码
                String code = baseMapper.queryCode(pid);
                maxCode = code + "01";
            }
        }
            else {
                maxCode = Integer.parseInt(maxCode)+1+"";
            }
        return maxCode;
    }
}
